package org.aaron.demos;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class AnimationsActivity extends Activity {

	private Button alphaBtn, scaleBtn, rotateBtn, translateBtn, xmlresuorceBtn;
	private ImageView imageView, imageView2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.animationslayout);
		initActivity();
	}

	private void initActivity() {
		alphaBtn = (Button) findViewById(R.id.alphaBtn);
		scaleBtn = (Button) findViewById(R.id.scaleBtn);
		rotateBtn = (Button) findViewById(R.id.rotateBtn);
		translateBtn = (Button) findViewById(R.id.translateBtn);
		xmlresuorceBtn = (Button) findViewById(R.id.xmlanimBtn);
		imageView = (ImageView) findViewById(R.id.myImageView);
		imageView2 = (ImageView) findViewById(R.id.myImageView2);
		alphaBtn.setOnClickListener(new AlphaBtnOnClickListener());
		scaleBtn.setOnClickListener(new ScaleBtnOnClickListener());
		rotateBtn.setOnClickListener(new RotateBtnOnClickListener());
		translateBtn.setOnClickListener(new TranslateOnClickListener());
		xmlresuorceBtn.setOnClickListener(new XmlResourceOnClickListener());
	}

	class AlphaBtnOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			// AnimationSet animationSet = new AnimationSet(true);
			// Animation animation = new AlphaAnimation(1F, 0F);
			// animation.setDuration(1000);
			// animationSet.addAnimation(animation);
			Animation animation = AnimationUtils.loadAnimation(AnimationsActivity.this, R.anim.animationinfo);
			imageView.startAnimation(animation);
		}
	}

	class ScaleBtnOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			AnimationSet animationSet = new AnimationSet(true);
			Animation animation = new ScaleAnimation(1.0F, 0.1F, 1.0F, 0.1F, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F);
			animation.setDuration(2000);
			animationSet.addAnimation(animation);
			imageView.startAnimation(animationSet);
		}
	}

	class RotateBtnOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			AnimationSet animationSet = new AnimationSet(true);
			Animation animation = new RotateAnimation(0F, 360F, Animation.RELATIVE_TO_PARENT, 0F, Animation.RELATIVE_TO_PARENT, 1F);

			animation.setDuration(2000);
			animationSet.addAnimation(animation);
			imageView.startAnimation(animationSet);
		}
	}

	class TranslateOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			AnimationSet animationSet = new AnimationSet(true);
			Animation animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0F, Animation.RELATIVE_TO_SELF, 1F);
			animation.setDuration(2000);
			animationSet.addAnimation(animation);
			imageView.startAnimation(animationSet);
		}
	}

	class XmlResourceOnClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			imageView2.setBackgroundResource(R.drawable.animlist);
			AnimationDrawable animationDrawable = (AnimationDrawable) imageView2.getBackground();
			animationDrawable.start();
		}
	}
}
